package com.eduhzy.ttw.clazz.mvp.presenter;

import android.app.Application;
import android.os.Message;

import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.clazz.mvp.contract.EditReviewContract;
import com.eduhzy.ttw.clazz.mvp.model.entity.ClazzScoreData;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.annotation.ScopeType;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.simple.eventbus.EventBus;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


/**
 * @author xubo
 * @date 2018-12-5 10:21:24
 */
@ActivityScope
public class EditReviewPresenter extends BasePresenter<EditReviewContract.Model, EditReviewContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    @Inject
    @ScopeType(2)
    BaseQuickAdapter<ClazzScoreData, AutoBaseViewHolder> mAdapter;

    @Inject
    public EditReviewPresenter(EditReviewContract.Model model, EditReviewContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }


    public void getEvaluateTagList(int classId, int scoreType) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("classId", classId);
        map.put("teacherId", SPUtils.getInstance().getString(Constants.USERID));
        map.put("scoreType", scoreType);
        mModel.getEvaluateTagList(map)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(disposable -> {
                    //显示下拉刷新的进度条
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    //隐藏下拉刷新的进度条
                    mRootView.hideLoading();
                })
                //使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<List<ClazzScoreData>>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<List<ClazzScoreData>> commonData) {
                        if (commonData.isResult()) {
                            mRootView.update(commonData.getData());
                        } else {
                            mRootView.showMessage(commonData.getMessage());
                        }
                    }
                });

    }

    public void evaluateTagDelete(int position, int classId, int tagId) {
        QMUITipDialog tipDialog = RxUtil.showLoading(mRootView.getActivity(), "正在删除···");
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("classId", classId);
        map.put("teacherId", SPUtils.getInstance().getString(Constants.USERID));
        map.put("tagId", tagId);
        //隐藏下拉刷新的进度条
        mModel.evaluateTagDelete(map)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(2, 3))
                .doOnSubscribe(disposable -> {
                    //显示下拉刷新的进度条
                    tipDialog.show();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(tipDialog::dismiss)
                //使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<Map>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<Map> commonData) {
                        if (commonData.isResult()) {
                            Message msg = Message.obtain();
                            msg.what = EventBusTags.DELETE_REVIEW;
                            EventBus.getDefault().post(msg, RouterHub.CLAZZ_EDITREVIEWFRAGMENT);
                            EventBus.getDefault().post(msg, RouterHub.CLAZZ_CLAZZREVIEWFRAGMENT);
                            EventBus.getDefault().post(msg, RouterHub.CLAZZ_GROUPREVIEWFRAGMENT);
                            mAdapter.remove(position);
                            if (mAdapter.getData().size() > position) {
                                mAdapter.notifyItemChanged(position);
                            } else {
                                mAdapter.notifyDataSetChanged();
                            }


                        }
                        mRootView.showMessage(commonData.getMessage());
                    }
                });
    }
}
